<?php
require_once('config.inc.php');

page_open( array( 'sess' => 'MySession'
                , 'auth' => 'MyAuth'
                )
         );

page_close();

/**
 * ROW BUDGET EMPTY
 */ 
function get_budget_empty($id_cat, $nom)
{
  return <<<EOBEM
  <span id="edit_$id_cat">Pas de budget</span>
  
  <script type="text/javascript">
  window.Budget.look_budget_empty("$id_cat", "edit_$id_cat");
  </script>
EOBEM;
}

/**
 * ROW BUDGET EXISTING
 */ 
function get_budget_existing($id_cat, $nom, $id_budget, $montant)
{
  return <<<EOBEX
  <span id="edit_$id_cat">$montant</span> €
  
  <script type="text/javascript">
  window.Budget.look_budget_existing("$id_cat", "$id_budget", "edit_$id_cat");
  </script>
EOBEX;
}

/**
 * ROW BUDGET
 */ 
function get_row_budget($id_cat, $nom, $id_budget, $montant)
{
  $budget = is_null($id_budget) ? get_budget_empty($id_cat, $nom)
                                : get_budget_existing($id_cat, $nom, $id_budget, $montant);
  return <<<EORB
  <tr id="row_cat_$id_cat">
    <td>$nom</td>
    <td id="montant_$id_cat">$budget</td></tr>

EORB;
}

// ACTIONS
switch (rd_get('action'))
{
case 'load':
  /// LOAD
  $budget = Gestion::get_budget($auth->auth['uid']);
  foreach ($budget as $b)
  {
    print get_row_budget( $b['id_categorie']
                        , $b['nom']
                        , $b['id_budget']
                        , $b['montant']
                        );
  }
  break;

case 'create':
  $id_cat    = rd_get('id_cat');
  $montant   = rd_get('value');
  $id_budget = Gestion::create_budget($auth->auth['uid'], $id_cat, $montant);
  if ($id_budget)
  {
    print <<<EOCB
  <script type="text/javascript">
  window.Budget.set_budget($id_cat, $id_budget, $montant);
  </script>
EOCB;
  }
  break;

case 'update':
  $id_cat    = rd_get('id_cat');
  $id_budget = rd_get('id_budget');
  $montant   = rd_get('value');
  if (strlen($montant) == 0)
  {
    if (Gestion::remove_budget($id_budget))
    {
      print <<<EORB
  <script type="text/javascript">
  window.Budget.remove_budget($id_cat);
  </script>
EORB;
    }
  }
  else if (Gestion::update_budget($id_budget, $montant))
  {
    print <<<EOUB
  <script type="text/javascript">
  window.Budget.set_budget($id_cat, $id_budget, $montant);
  </script>
EOUB;
  }
  break;
}
?>
